package gl.util

import org.lwjgl.PointerBuffer
import org.lwjgl.system.MemoryStack
import java.nio.*

fun ByteArray.toBuffer(): ByteBuffer = MemoryStack.stackGet().bytes(*this)
fun ShortArray.toBuffer(): ShortBuffer = MemoryStack.stackGet().shorts(*this)
fun IntArray.toBuffer(): IntBuffer = MemoryStack.stackGet().ints(*this)

fun Array<IntArray>.toPointer(mem: MemoryStack = MemoryStack.stackGet()): PointerBuffer {
    val arrayBuffer = Array<Buffer>(size) {
        return@Array this[it].toBuffer()
    }
    return mem.pointers(*arrayBuffer)
}

fun ByteArray.string(): String {
    val sb = StringBuilder()
    for (b in this) {
        if (b == '\u0000'.toByte()) {
            return sb.toString()
        }
        sb.append(b.toChar())
    }
    return sb.toString()
}